package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.dto.PasswordEditDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "员工登录")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation(value = "员工退出登录")
    public Result<String> logout() {
        return Result.success();
    }

    @PostMapping
    @ApiOperation(value = "新增员工")
    public Result<EmployeeDTO> create(@RequestBody EmployeeDTO employeeDTO) {

//        System.out.println("current thread id" + Thread.currentThread().getId());

        log.info("新增员工：{}", employeeDTO);

        // 调用业务层代码插入数据
        employeeService.create(employeeDTO);

        // 插入成功，返回
        return Result.success(employeeDTO);

    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询员工")
    public Result<PageResult> pageQuery(EmployeePageQueryDTO employeePageQueryDTO) {

        // 记录日志
        log.info("分页查询员工：{}", employeePageQueryDTO);

        // 调用业务层，查询结果
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);

        // 返回查询结果
        return Result.success(pageResult);

    }


    @PostMapping("/status/{status}")
    @ApiOperation(value = "转换员工状态")
    public Result<Employee> transitionStatus(@PathVariable("status") Integer status, Long id) {

        // 日志输出
        log.info("转换员工状态，员工信息：{}，{}", id, status);

        // 调用业务层转换状态
        Employee employee = employeeService.transitionStatus(status, id);

        // 返回修改后的数据
        return Result.success(employee);

    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据 id 查询员工")
    public Result<Employee> queryById(@PathVariable("id") Long id) {
        // 输出日志
        log.info("根据 id 查询员工：{}", id);

        // 调用业务层查询
        Employee employee = employeeService.queryById(id);

        // 返回查询结果
        return Result.success(employee);

    }

    @PutMapping()
    @ApiOperation(value = "修改员工信息")
    public Result<Employee> updateById(@RequestBody EmployeeDTO employeeDTO) {
        // 输出日志
        log.info("修改员工信息：{}", employeeDTO);

        // 调用业务层
        Employee employee = employeeService.updateById(employeeDTO);

        // 返回修改后的数据
        return Result.success(employee);
    }

    @PutMapping("/editPassword")
    @ApiOperation(value = "修改登录密码")
    public Result<Employee> updatePassword(@RequestBody PasswordEditDTO passwordEditDTO) {
        // 输出日志
        // TODO 接口测试通过，但无法接收前端 id
        log.info("修改登录密码：{}", passwordEditDTO);

        // 调用业务层

        Employee employee = employeeService.updatePassword(passwordEditDTO);

        return Result.success(employee);

    }



}
